package People;

import course.Course;

public class Student extends People {
    private static final int LAB_CONTAINER_SIZE_INCREMENT = 5;

    private Course[] studentCourse;

    private int currentNumberOfCourses;

    public Student() {}

    public Student(int id, String name, String gender) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        studentCourse = new Course[LAB_CONTAINER_SIZE_INCREMENT];
    }

    public void chooseCourse(Course course) {
        for (int i = 0; i<this.currentNumberOfCourses; i++) {
            if (this.studentCourse[i].equals(course)) {
                return;
            }
        }
        this.studentCourse[this.currentNumberOfCourses] = course;
        this.currentNumberOfCourses++;
        if (this.currentNumberOfCourses == this.studentCourse.length) {
            Course[] courses = new Course[this.currentNumberOfCourses + LAB_CONTAINER_SIZE_INCREMENT];
            System.arraycopy(this.studentCourse, 0, courses, 0, this.currentNumberOfCourses);
            this.studentCourse = courses;
        }
    }//学生选课

    public void removeCourse(Course course) {
        for (int i = 0; i < this.currentNumberOfCourses; i++) {
            if (this.studentCourse[i].equals(course)) {
                if (this.currentNumberOfCourses - 1 - i >= 0) {
                        System.arraycopy(this.studentCourse, i + 1, this.studentCourse, i, this.currentNumberOfCourses - 1 - i);
                        this.studentCourse[this.currentNumberOfCourses - 1] = null;
                        this.currentNumberOfCourses --;
                    }
                return;
            }
        }
    }//学生退课

    public void displayCourse(){
        System.out.print("学生"+this.name +"所选课程有：");
        for(Course c: studentCourse){
            if(c!=null)
                System.out.println("\n "+c.getCourseId()+" "+c.getCourseName()+" "
                        +c.getClassroom()+" "+c.getTime()+" ");}
    }//打印学生课表

    @Override
    public String toString() {
        StringBuilder personInfo = new StringBuilder(super.toString());
        for (Course course : this.studentCourse) {
            personInfo.append("\n")
                    .append("course: ")
                    .append(course)
                    .append("\n");
        }
        return personInfo.toString();
    }

}
